#ifndef BUTTON_H
#define BUTTON_H

#include <stdint.h>
#include <stdbool.h>

#define TOUCH_ENABLE_TIME_SEC (120)

typedef enum
{
#if (defined(BUTTONS_NUM) && BUTTONS_NUM >= 1)
    BUTTON_NUM_1,
#endif
#if (defined(BUTTONS_NUM) && BUTTONS_NUM >= 2)
    BUTTON_NUM_2,
#endif
#if (defined(BUTTONS_NUM) && BUTTONS_NUM >= 3)
    BUTTON_NUM_3,
#endif
#if (defined(BUTTONS_NUM) && BUTTONS_NUM >= 4)
    BUTTON_NUM_4,
#endif
#if (defined(BUTTONS_NUM) && BUTTONS_NUM >= 5)
    BUTTON_NUM_5,
#endif
#if (defined(BUTTONS_NUM) && BUTTONS_NUM >= 6)
    BUTTON_NUM_6,
#endif
#ifdef USE_TOUCHPAD
    BUTTON_TOUCH,
    event_source_touch = BUTTON_TOUCH,
#endif
    event_source_system,
    event_source_gsensor,
#if defined(R911_V1_1_BOARD) || defined(R911_V1_11_BOARD) || defined(Z1_V1_11_BOARD) || defined(R911M_V1_11_BOARD) || defined(R911M2_V1_11_BOARD)
#if !defined(BUTTON_TOP) || !defined(BUTTON_MIDDLE) || !defined(BUTTON_BOTTOM)
#error not defined buttons
#endif
    event_source_button_top = BUTTON_NUM_1,
    event_source_button_middle = BUTTON_NUM_2,
    event_source_button_bottom = BUTTON_NUM_3,
#endif
#if defined(R33_BOARD) || defined(SM02_BOARD)
    event_source_button_front = BUTTON_NUM_1,
    event_source_button_side = BUTTON_NUM_2,
#endif
#if defined(R66_BOARD)
    event_source_button_side = BUTTON_NUM_1,
#endif
#if defined(R35_BOARD)
    event_source_button_front = BUTTON_NUM_1,
    event_source_button_up = BUTTON_NUM_2,
    event_source_button_down = BUTTON_NUM_3,
    event_source_button_back = BUTTON_NUM_4,
    event_source_button_enter = BUTTON_NUM_5,
#endif
#if defined(IT_POWER_01_BOARD)
    event_source_button_front = BUTTON_NUM_1,
#endif
#if defined(R30_BOARD) || defined(Z2_BOARD) || defined(R5MAX_BOARD) || defined(R5_BOARD)|| defined(SP2_BOARD)
    event_source_button_front = BUTTON_NUM_1,
#endif
#if defined(R32_BOARD) || defined(R32_BOARD_V1_1) || defined(BT100_BOARD)
    event_source_button_front = BUTTON_NUM_1,
#endif
#if defined(R31_BOARD) || defined(SW03_BOARD)
    event_source_button_front = BUTTON_NUM_1,
    event_source_button_up = BUTTON_NUM_2,
    event_source_button_down = BUTTON_NUM_3,
    event_source_button_back = BUTTON_NUM_4,
    event_source_button_enter = BUTTON_NUM_5,
#endif
#if defined(PROJECT_X3_E)
    event_source_button_x3_e_top = BUTTON_NUM_2,
    event_source_button_x3_e_bottom = BUTTON_NUM_1,
#endif
#if defined(BOARD_L32)
    event_source_button_front = BUTTON_NUM_1,
    event_source_button_up = BUTTON_NUM_2,
    event_source_button_side = BUTTON_NUM_3,
    event_source_button_down = BUTTON_NUM_4,
    event_source_button_enter = BUTTON_NUM_6,
    event_source_button_back = BUTTON_NUM_5,
#endif
} button_num_t;

typedef button_num_t event_source_t;

typedef enum
{
    BUTTON_EVENT_NONE,
    BUTTON_EVENT_SHORT_PRESS, //短按
    BUTTON_EVENT_LONG_PRESS,  //长按
    BUTTON_EVENT_LONG_PRESS_KEEP,
#ifdef USE_TOUCHPAD
    BUTTON_EVENT_TP_TAP,
    BUTTON_EVENT_TP_TAP_LONG,
    BUTTON_EVENT_TP_MOVE_UP,
    BUTTON_EVENT_TP_MOVE_DOWN,
    BUTTON_EVENT_TP_MOVE_LEFT,
    BUTTON_EVENT_TP_MOVE_RIGHT,
#endif
#ifdef EVENT_PREVIOUS_VIEW
    event_move_previous_view,
#endif
#ifdef EVENT_NEXT_VIEW
    event_move_next_view,
#endif
#ifdef EVENT_LEVEL_UP
    event_move_level_up,
#endif
#ifdef EVENT_LEVEL_DOWN
    event_move_level_down,
#endif
#ifdef EVENT_BUTTON_FRONT
    event_button_front_press,
    event_button_front_long_press,
#endif
    event_view_entry,
    event_view_exit,
    event_view_re_screen_on,
    event_shake,
    event_ble_connect,
    event_ble_disconnect,
#ifdef USE_BLE_METRONOME_HZ_SET
    event_set_metronome,
#endif
} button_event_t;

typedef button_event_t event_t;

typedef struct __event_param_t
{
    event_source_t source;
    event_t event;
    union
    {
        int next_view;
        int previous_view;
        int x;
        int hz;
        int long_press_millisecond;
    };
    union
    {
        int y;
    };
} event_param_t;

void button_event_handle(void);
void button_event_active_value(button_num_t num, button_event_t event, int param_0, int param_1);
void ButtonInit(void);

void pedo_turn_wrist_hook(uint8_t event);
void shake_check(int x, int y, int z);
void touch_process(void);
#endif
